package com.dlws.common.utils;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.dlws.common.web.BaseController;


/**
 * 
 * 描述：上传图片 
 * 创建人： 魏泽超 
 * 创建时间: 2016-4-19 下午1:54:44
 * 
 * @version
 */
@Controller
@RequestMapping("/upload")
public class UploadController extends BaseController{

	/**
	 * 
	 * 描述：我的能力上传图片
	 * 创建人： 魏泽超
	 * 创建时间: 2016-4-25 下午1:08:16
	 * @version
	 */
	@RequestMapping(value = "/uploadcpImg.do")
	public void uploadcpImg(@RequestParam(required = false) MultipartFile[] pic, HttpServletResponse response,HttpServletRequest request) {
		for (MultipartFile file : pic) {
			System.out.println(file.getOriginalFilename());
			JSONObject jo = new JSONObject();
			boolean flag=checkFile(file.getOriginalFilename());
			jo.put("flag", flag);
			// 扩展名
			String ext = FilenameUtils.getExtension(file.getOriginalFilename()).trim().toLowerCase();

			// 图片名称生成策略
			DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
			// 图片名称一部分
			String name = df.format(new Date());

			// 随机三位数
			Random r = new Random();
			// n 1000 0-999 99
			for (int i = 0; i < 3; i++) {
				name += r.nextInt(10);
			}
			
			//实例化一个Jersey
			//Client client = new Client();
			//另一台服务器的请求路径是?
			//String url = Constants.IMAGE_URL  + path;
			//设置请求路径
			//WebResource resource = client.resource(url);
			//
			String pathUrl= request.getServletContext().getRealPath("/");
			// 保存到数据库的路径
			String path = pathUrl +"/cpimg/" + name + "." + ext;
			String url="/cpimg/" + name + "." + ext;
			File f = new File(path);
			// 发送图片
			try {
				if(flag){
					file.transferTo(f);
					System.out.println("图片上传成功");
				}
				// resource.put(String.class, pic.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// 返回路径
			response.setContentType("application/json;charset=UTF-8");

			// springmvc json
			
			jo.put("path", url);

			try {
				response.getWriter().write(jo.toString());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 
	 * 描述：指定文件类型
	 * 创建人： 魏泽超
	 * 创建时间: 2016-4-21 下午5:24:52
	 * @version
	 */
	private  boolean checkFile(String fileName){
		boolean flag=false;
		String suffixList="jpg,gif,png,ico,bmp,jpeg";
		//获取文件后缀
		String suffix=fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
		
		if(suffixList.contains(suffix.trim().toLowerCase())){
			flag=true;
		}
		return flag;
	}
	
}
